package com.ssyt.examinationsystem.controller;

import com.alibaba.fastjson.JSON;
import com.ssyt.examinationsystem.entity.PaperInfo;
import com.ssyt.examinationsystem.entity.ScoreInfo;
import com.ssyt.examinationsystem.entity.StudentInfo;
import com.ssyt.examinationsystem.service.IPaperInfoService;
import com.ssyt.examinationsystem.service.IScoreInfoService;
import com.ssyt.examinationsystem.service.IStudentInfoService;
import com.ssyt.examinationsystem.service.impl.PaperInfoServiceImpl;
import com.ssyt.examinationsystem.service.impl.ScoreInfoServiceImpl;
import com.ssyt.examinationsystem.service.impl.StudentInfoServiceImpl;
import com.ssyt.examinationsystem.util.CollectionUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class ScoreInfoController
 */
public class ScoreInfoController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ScoreInfoController() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 调用成绩业务方法
        IScoreInfoService scoreService = new ScoreInfoServiceImpl();

        IPaperInfoService paperService = new PaperInfoServiceImpl();
        IStudentInfoService stuService = new StudentInfoServiceImpl();
        PrintWriter out = response.getWriter();
        // 获取地址栏上的flag值
        String flag = request.getParameter("flag");
        if ("query".equals(flag)) {
            query(request, response);
        } else if ("lead".equals(flag)) {
            lead(request, response);
        } else if ("list".equals(flag)) {
            list(stuService, paperService, scoreService, request, response, out);
        } else if ("add".equals(flag)) {
            add(scoreService, request, response, out);
        } else if ("dele".equals(flag)) {
            delete(scoreService, request, response, out);
        } else if ("update".equals(flag)) {
            update(scoreService, request, response, out);
        }

        out.flush();
        out.close();
    }

    /**
     * 修改成绩信息
     *
     * @param scoreService
     * @param request
     * @param response
     * @param out
     */
    private void update(IScoreInfoService scoreService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        // 获取表单数据
        String scoreId = request.getParameter("scoreId");
        String paperId = request.getParameter("paperId");
        String stuId = request.getParameter("stuId");
        String objectiveScore = request.getParameter("objectiveScore");
        String subjectiveScore = request.getParameter("subjectiveScore");
        // 创建一个成绩对象，并封装属性
        ScoreInfo score = new ScoreInfo();
        score.setScoreId(Integer.valueOf(scoreId));

        PaperInfo paper = new PaperInfo();
        paper.setPaperId(Integer.valueOf(paperId));
        score.setPaper(paper);

        StudentInfo stu = new StudentInfo();
        stu.setStuId(Integer.valueOf(stuId));
        score.setStu(stu);

        score.setObjectiveScore(Float.valueOf(objectiveScore));
        score.setSubjectiveScore(Float.valueOf(subjectiveScore));

        int row = scoreService.updateScore(score);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 删除成绩信息
     *
     * @param scoreService
     * @param request
     * @param response
     * @param out
     */
    private void delete(IScoreInfoService scoreService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        String scoreId = request.getParameter("scoreId");
        // 调用并判断删除成绩信息
        if (scoreService.deleteScore(scoreId) > 0) {
            out.print("ok");
        }
    }

    /**
     * 添加成绩信息
     *
     * @param scoreService
     * @param request
     * @param response
     * @param out
     */
    private void add(IScoreInfoService scoreService, HttpServletRequest request, HttpServletResponse response,
                     PrintWriter out) {
        // 获取表单数据
        String paperId = request.getParameter("paperId");
        String stuId = request.getParameter("stuId");
        String objectiveScore = request.getParameter("objectiveScore");
        String subjectiveScore = request.getParameter("subjectiveScore");
        // 创建一个成绩对象，并封装属性
        ScoreInfo score = new ScoreInfo();

        PaperInfo paper = new PaperInfo();
        paper.setPaperId(Integer.valueOf(paperId));
        score.setPaper(paper);

        StudentInfo stu = new StudentInfo();
        stu.setStuId(Integer.valueOf(stuId));
        score.setStu(stu);

        score.setObjectiveScore(Float.valueOf(objectiveScore));
        score.setSubjectiveScore(Float.valueOf(subjectiveScore));

        int row = scoreService.addScore(score);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 显示成绩信息
     *
     * @param scoreService
     * @param request
     * @param response
     * @param out
     */
    private void list(IStudentInfoService stuService, IPaperInfoService paperService, IScoreInfoService scoreService, HttpServletRequest request, HttpServletResponse response,
                      PrintWriter out) {
        // 调用查询所有成绩信息
        List<Map<String, Object>> scoreList = scoreService.getScores();
        if (CollectionUtils.isEmptyForList(scoreList)) {
            for (Map<String, Object> map : scoreList) {
                float totalScore = Float.valueOf(String.valueOf(map.get("objectiveScore")))
                        + Float.valueOf(String.valueOf(map.get("subjectiveScore")));
                map.put("totalScore", totalScore);

                String stuId = String.valueOf(map.get("stuId"));
                String paperId = String.valueOf(map.get("paperId"));

                Map<String, Object> student = stuService.getStuByStuId(stuId);
                String stuName = String.valueOf(student.get("stuName"));
                map.put("stuName", stuName);

                String paperName = paperService.getPaperName(paperId);
                map.put("paperName", paperName);
            }
        }

        String scoreJson = JSON.toJSONString(scoreList, true);
        out.print(scoreJson);
    }

    /**
     * 转发到导入成绩信息页面
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void lead(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/views/score/scoreLead.jsp").forward(request, response);
    }

    /**
     * 转发到维护页面
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/views/score/scoreList.jsp").forward(request, response);

    }

}
